package com.lagou.homework.modulefour.third;

/*
 * @className MyThread
 * @description
 * @author admin
 * @date 2022/4/1 16:26
 * */
public class MyThread implements Runnable {
    private Train train;
    private int number;

    // 无参构造
    public MyThread() {
    }

    // 有参构造
    public MyThread(Train train) {
        this.train = train;
        this.number = train.getNumber();
    }

    @Override
    public void run() {
        while(true) {
            synchronized (MyThread.class){
                if (number++ < 100) {
                    System.out.println("线程" + Thread.currentThread().getName() + "购买了第" + number + "张火车票!");
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    System.out.println("火车票已售完！");
                    break;
                }
            }
        }
    }
}
